/**
 * 
 */
package com.xsoft.framework.common;

import java.sql.Connection;
import java.sql.SQLException;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.StatementScope;

/**
 * @author 潘冬
 */
public class OracleSqlExecutor extends SqlExecutor {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ibatis.sqlmap.engine.execution.SqlExecutorexecuteQuery(com.ibatis
	 * .sqlmap.engine.scope.RequestScope, java.sql.Connection, java.lang.String,
	 * java.lang.Object[], int, int,
	 * com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback)
	 */
	public void executeQuery(StatementScope arg0, Connection arg1, String arg2,
			Object[] arg3, int arg4, int arg5, RowHandlerCallback arg6)
			throws SQLException {
		// TODO Auto-generated method stub
		if ((arg4 != NO_SKIPPED_RESULTS || arg5 != NO_MAXIMUM_RESULTS)) {
			StringBuffer sql_tmp = new StringBuffer(
					"select * from (select a.*, rownum num from (");
			sql_tmp.append(arg2);
			sql_tmp.append(") a where rownum <= "+(arg5 + arg4)+") where num >"+arg4);
//			int orderByIndex = arg2.toLowerCase().indexOf("order");
//			sql_tmp.append(arg2.substring(
//					arg2.toLowerCase().indexOf("select") + 7,
//					orderByIndex > -1 ? orderByIndex : arg2.length()));
//			if (arg2.indexOf("where") > -1) {
//				sql_tmp.append(" and rownum<=" + (arg5 + arg4));
//			} else {
//				sql_tmp.append(" where rownum<=" + (arg5 + arg4));
//			}
//			if (orderByIndex > -1) {
//				sql_tmp.append(" "
//						+ arg2.substring(orderByIndex, arg2.length()));
//			}
//			sql_tmp.append(") where num>" + arg4);
			arg2 = sql_tmp.toString();
			arg4 = NO_SKIPPED_RESULTS;
			arg5 = NO_MAXIMUM_RESULTS;
		}
		super.executeQuery(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
	}

}
